package com.powerbridge.bls.cod.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.dom4j.Element;

import java.util.Iterator;
import java.util.List;

public class Document2MapUtil {
    //解析xml文件节点结构及内容，生成JSON字符串
    public static void iterateNodes(Element node, JSONObject json) {
        String nodeName = node.getName();
        JSONObject object;
        if (json.containsKey(nodeName)) {
            Object Object = json.get(nodeName);
            JSONArray array;
            if (Object instanceof JSONArray) {
                array = (JSONArray)Object;
            } else {
                array = new JSONArray();
                array.add(Object);
            }

            List<Element> listElement = node.elements();
            if (listElement.isEmpty()) {
                String nodeValue = node.getTextTrim();
                array.add(nodeValue);
                json.put(nodeName, array);
            } else {
                JSONObject newJson = new JSONObject();
                Iterator var8 = listElement.iterator();

                while(var8.hasNext()) {
                    Element e = (Element)var8.next();
                    iterateNodes(e, newJson);
                }

                array.add(newJson);
                json.put(nodeName, array);
            }
        } else {
            List<Element> listElement = node.elements();
            if (listElement.isEmpty()) {
                String nodeValue = node.getTextTrim();
                json.put(nodeName, nodeValue);
            } else {
                object = new JSONObject();
                Iterator var6 = listElement.iterator();

                while(var6.hasNext()) {
                    Element e = (Element)var6.next();
                    iterateNodes(e, object);
                }

                json.put(nodeName, object);
            }
        }
    }



}
